package com.windy.dailynews.request;

import android.util.Log;

import com.windy.dailynews.request.converter.JsonConverterFactory;

import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dingfeng on 2016/11/21.
 */
public class DataRequest {
    private static final String TAG = DataRequest.class.getSimpleName();
    private static DataRequest instance;
    private Retrofit mRetrofit;
    private ApiService mApiService;

    public static synchronized DataRequest getInstance() {
        if (instance == null) {
            instance = new DataRequest();
        }
        return instance;
    }

    private DataRequest() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(ApiConstant.BASE_URL)
                .addConverterFactory(JsonConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(RetrofitUtil.genericClient())
                .build();
        mApiService = mRetrofit.create(ApiService.class);
    }

    //请求数据回调
    public interface Callback<T> {
        void onSuccess(T data);

        void onError(String err);
    }

    public void getLatestNews(final Callback<JSONObject> callback) {
        mApiService.getLatestNews().enqueue(new retrofit2.Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                JSONObject object = response.body();
                callback.onSuccess(object);
            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    public void getBeforeNews(String date, final Callback<JSONObject> callback) {
        mApiService.getBeforeNews(date).enqueue(new retrofit2.Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                JSONObject object = response.body();
                callback.onSuccess(object);
            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    public void getNewsContent(int id, final Callback<JSONObject> callback) {
        mApiService.getNewsContent(id).enqueue(new retrofit2.Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                JSONObject object = response.body();
                callback.onSuccess(object);
            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    public void getMenuList(final Callback<JSONObject> callback) {
        mApiService.getMenuList().enqueue(new retrofit2.Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                JSONObject object = response.body();
                if (object == null) return;
                callback.onSuccess(object);
            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

    public void getThemeNewsContent(int id, final Callback<JSONObject> callback) {
        mApiService.getThemeNewsContent(id).enqueue(new retrofit2.Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                JSONObject object = response.body();
                callback.onSuccess(object);
            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
    }

}
